<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!-- 搜索栏-->
      <!--<div v-if="crud.props.searchToggle">
        <el-input v-model="query.title" clearable size="small" placeholder="输入组织名称搜索" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation />
      </div>-->
      <!-- 按钮-->
      <crud-opts :permission="permission" />
    </div>
    <!-- 表格-->
    <!-- 如果需要指定表格高度，可以在el-table 中style 属性后 加入属性 height="70vh"-->
    <!--
      如果是树形 tree -table ，加上以下两个属性
      @select="crud.selectChange"
      @select-all="crud.selectAllChange"
      @selection-change="crud.selectionChangeHandler"
    -->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      border
      style="width: 100% "
      row-key="id"
      stripe
      lazy
      :load="loadChildren"
      :tree-props="{children: 'children', hasChildren: 'id' }"
      tooltip-effect="dark"
      :highlight-current-row="true"
      @sort-change="crud.sortChange"
      @selection-change="crud.selectionChangeHandler"
      @select="crud.selectChange"
      @select-all="crud.selectAllChange"
    >
      <el-table-column type="selection" width="39" />
      <el-table-column prop="key" label="索引(key)" />
      <el-table-column prop="value" label="字典值(value)" />
      <el-table-column prop="dictLabel" label="所属字典标识" />
      <el-table-column
        prop="status"
        label="状态"
        width="80"
        sortable="custom"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            class="switch"
            :width="50"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="启用"
            inactive-text="禁用"
            :disabled="!permission.has('edit')"
            @change="crud.changeStatusWithNum(scope.row, scope.row.status, {'title':'字典值','column':'value'})"
          />
        </template>
      </el-table-column>
      <el-table-column prop="paixu" label="排序" width="80" header-align="center" align="center" sortable="custom" />
      <el-table-column prop="createTime" label="创建时间" :show-overflow-tooltip="true" header-align="center" sortable="custom" />
      <el-table-column fixed="right" label="操作" width="120" header-align="center" align="center">
        <template slot-scope="scope">
          <rudOperation
            :data="scope.row"
            :permission="permission"
          >
            <template slot="top">
              <el-button
                size="mini"
                type="success"
                @click="addChild(scope.row)"
              >
                新增下级
              </el-button>
            </template>
          </rudOperation>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页插件, 如果不需要注释掉下面一行,以及引入的 pagination 组件即可 -->
    <!--<pagination />-->
    <!-- 表单 -->
    <SysDictForm ref="detailForm" :dict-type="1" :dict-label="dictLabel" :parent-id="parentId" />
    <!-- 详情 -->
    <SysDictInfo />
  </div>
</template>

<script>

import crudApi, { list } from '@/api/sys/Dict'
import SysDictForm from './form'
import SysDictInfo from './info'
import CRUD, { presenter, header } from '@crud/crud'
import CrudOpts from '@crud/CRUD.operation'
// import rrOperation from '@crud/RR.operation'
import rudOperation from '@crud/RUD.operation'
// 系统字典
export default {
  components: {
    CrudOpts,
    // rrOperation,
    rudOperation,
    SysDictForm,
    SysDictInfo
  },
  cruds() {
    return CRUD({ title: '字典详情', initDataMethod: list, crudMethod: { ...crudApi },
      optShow: {
        download: false,
        reset: false,
        rightBar: true
      }
    })
  },
  mixins: [presenter(), header()],
  props: {
    parentId: {
      type: String, // 上级id
      default: () => { return '0' }
    },
    dictLabel: {
      type: String, // 字典标签值
      default: () => { return null }
    }
  },
  data() {
    return {
      permission: new Map(),
      addChildDict: false
    }
  },
  watch: {
    parentId(newData) { // 监听选中的菜单
      if (newData && newData !== '0') {
        this.crud.refresh()
      }
    }
  },
  mounted() {
    this.permission = this.$store.state.menu.buttonPermission.get('SysDict')
  },
  methods: {
    [CRUD.HOOK.beforeRefresh]() {
      this.query.parentId = this.parentId
      this.query.dictType = 1
      this.query.dictLabel = this.dictLabel
      return true
    },
    addChild(data) { // 在字典值添加下级
      this.$refs.detailForm.addChildDict = true
      this.$refs.detailForm.parentInfo = data
      this.crud.toAdd()
    },
    // 加载子节点
    loadChildren(row, treeNode, resolve) {
      this.query.dictType = row.dictType
      this.query.dictLabel = row.dictLabel
      this.query.parentId = row.id
      // this.loadedChildren = true // 已经加载子项
      list(this.query).then(res => {
        if (res.data) {
          resolve(res.data)
        } else {
          resolve([])
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
